<link rel="stylesheet" type="text/css" href="<?php echo COMPONENT_PATH."css/jquery.tabs.css"; ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo COMPONENT_PATH."css/jquery.tabs-ie.css"; ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo COMPONENT_PATH."css/datepicker.css"; ?>" />
<table border="0">
	<tr class="HeaderTr">
		<td><b><?php echo "Unit Address";?></b></td>
	</tr>
	<tr><td>
	<form action="" method="post" name="edit_booking_form">
	<div id="bookings_tabs">
		<ul>
			<li><a href="#reservation_info"><span>Reservation Info</span></a></li>
			<li><a href="#payments"><span>Payments</span></a></li>
			<li><a href="#guest_info"><span>Guest Info</span></a></li>
			<li><a href="#set_letters"><span>Set Letters</span></a></li>
		</ul>
		<div id="reservation_info">
			<table width="100%" border="0" align="left">
			  <tr>
			    <td colspan="2" valign="top">
			    <table width="100%" border="0">
			      <tr>
			        <td valign="top" colspan="2">
			        	<b>Reference: <?php echo $bookings->id;?></b>
			        	<input type="hidden" name="bookings[cid]" value="<?php echo $bookings->cid;?>" />
			        </td>
			        <td width="33%" valign="top">
			        	<label for="confirmed">Confirmed: </label>
			            <select name="bookings[confirmed]" id="confirmed">
			            <option value="">Select</option>
			            <option value="0" <?php echo ($bookings->confirmed == 0 ? 'selected="selected"' : '' );?>>No</option>
			            <option value="1" <?php echo ($bookings->confirmed == 1 ? 'selected="selected"' : '' );?>>Yes</option>
			            </select>
			        </td>
			      </tr>
			      <tr>
			        <td width="15%" valign="top">Arrival Date: </td>
			        <td valign="top"><input type="text" name="bookings[startdate]" id="arr_date" value="<?php echo get_string_from_date($bookings->startdate);?>" class="date_pick" /></td>
			        <td valign="top">&nbsp;</td>
			      </tr>
			      <tr>
			        <td valign="top">Departure Date: </td>
			        <td valign="top"><input type="text" name="bookings[enddate]" id="dep_date" value="<?php echo get_string_from_date($bookings->enddate);?>" class="date_pick" /></td>
			        <td valign="top">&nbsp;</td>
			      </tr>
			    </table></td>
			    <td width="22%" valign="top">
			    	<!-- <input type="button" value="Send confirmation email to owner" width="3000">
			    	<input type="button" value="Send guest confirmation">
			    	<input type="button" value="Send security deposit request">
			    	<input type="button" value="Send owners cheque"> -->
			    </td>
			  </tr>
			  <tr>
			    <td colspan="3" valign="top"><hr /></td>
			  </tr>
			  <tr>
			    <td colspan="2" valign="top">Deposit Amount:</td>
			    <td valign="top"><?php echo $total_price*0.2; ?></td>
			  </tr>
			  <tr>
			    <td colspan="2" valign="top">Balance Amount:</td>
			    <td valign="top"><?php echo ($total_price - $bookings->total_paid); ?></td>
			  </tr>
			  <tr>
			    <td colspan="2" valign="top">CC Fees:</td>
			    <td valign="top"><?php echo $total_price*0.04; ?></td>
			  </tr>
			  <tr>
			    <td colspan="2" valign="top">Commision Amount:</td>
			    <td valign="top"><?php echo $total_price*0.15; ?></td>
			  </tr>
			  <tr>
			    <td colspan="2" valign="top"><p>Balance Due date:</p></td>
			    <td valign="top"><i>What is this?</i></td>
			  </tr>
			  <tr>
			    <td colspan="3" valign="top"><hr /></td>
			  </tr>
			  <tr>
			    <td colspan="2" valign="top">Sub Total:</td>
			    <td valign="top"><input type="text" name="subtotal" size="10" value="<?php echo $subtotal;?>" /></td>
			  </tr>
			  <tr>
			    <td colspan="2" valign="top">Discount($):</td>
			    <td valign="top"><input type="text" name="bookings[discount]" size="10" value="<?php echo $bookings->discount;?>" /></td>
			  </tr>
			  <tr>
			    <td colspan="2" valign="top">Total($):</td>
			    <td valign="top"><input type="text" name="total" size="10" value="<?php echo ($subtotal-$bookings->discount);?>" readonly /></td>
			  </tr>
			  <tr>
			    <td colspan="2" valign="top">Total Paid:</td>
			    <td valign="top"><input type="text" name="bookings[total_paid]" size="10" value="<?php echo $bookings->total_paid;?>" readonly /></td>
			  </tr>
			  <tr>
			    <td colspan="3" valign="top"><hr /></td>
			  </tr>
			  <tr>
			    <td width="65%" valign="top">Security Deposit Received: Cheque Number <input type="text" name="bookings[security_deposit_received_number]" value="<?php echo $bookings->security_deposit_received_number; ?>" /></td>
			    <td width="13%" align="right" valign="top">Date: </td>
			    <td valign="top"><input type="text" name="bookings[sd_received_date]" class="date_pick" value="<?php echo $bookings->sd_received_date;?>" /></td>
			  </tr>
			  <tr>
			    <td valign="top">Security Deposit Returned: Cheque Number <input type="text" name="bookings[security_deposit_returned_number]" value="<?php echo $bookings->security_deposit_returned_number; ?>" /></td>
			    <td align="right" valign="top">Date: </td>
			    <td valign="top"><input type="text" name="bookings[sd_returned_date]" class="date_pick" value="<?php echo $bookings->sd_returned_date;?>" /></td>
			  </tr>
			  <tr>
			    <td colspan="3" valign="top"><hr /></td>
			  </tr>
			  <tr>
			    <td valign="top">Deposit to owner sent: Cheque Number <input type="text" name="bookings[deposit_sent_to_owner_number]" value="<?php echo $bookings->deposit_sent_to_owner_number; ?>" /></td>
			    <td align="right" valign="top">Date: </td>
			    <td valign="top"><input type="text" name="bookings[owner_deposit_sent_date]" class="date_pick" value="<?php echo $bookings->owner_deposit_sent_date;?>" /></td>
			  </tr>
			  <tr>
			    <td valign="top">Balance to owner sent: Cheque Number <input type="text" name="bookings[balance_sent_to_owner_number]" value="<?php echo $bookings->balance_sent_to_owner_number; ?>" /></td>
			    <td align="right" valign="top">Date: </td>
			    <td valign="top"><input type="text" name="bookings[owner_balance_sent_date]" class="date_pick" value="<?php echo $bookings->owner_balance_sent_date;?>" /></td>
			  </tr>
			  <tr>
			    <td colspan="3" valign="top"><hr /></td>
			  </tr>
			  <tr>
			    <td colspan="3" valign="top">Notes:    
			    <textarea name="bookings[notes]" cols="100"><?php echo $bookings->notes; ?></textarea></td>
			  </tr>
			  <tr>
			    <td colspan="3" valign="top"><hr /></td>
			  </tr>
			  <tr>
			    <td colspan="2" valign="top">
			    	Unit: 
			        <select name="bookings[uid]" id="unit">
			        <option value="">Select A Unit</option>
			        <?php 
			        	foreach($units as $unit){
			        ?>
			        		<option value="<?php echo $unit->id;?>" <?php echo ($unit->id == $bookings->uid ? "selected=\"selected\"" : "");?>><?php echo $unit->address;?></option>
			        <?php 
			        	}
			        ?>
			        </select>
			    </td>
			    <td valign="top">&nbsp;</td>
			  </tr>
			</table>
		</div>
		<div id="payments">
			<table width="100%" border="0">
			  <tr>
			    <td><b>Reference: <?php echo $bookings->id;?></b></td>
			  </tr>
			  <tr><td>&nbsp;</td></tr>
			  <tr>
			    <td>
			    <table width="100%" border="0">
			      <tr>
			        <td width="10%">CC Number:</td>
			        <td width="58%">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name="cc_info[number]" value="<?php echo $cc_info->number;?>" /></td>
			        <td width="14%">Cardholder:</td>
			        <td width="18%" align="right"><input name="cc_info[card_holder]" id="card_holder" value="<?php echo $cc_info->card_holder;?>" /></td>
			      </tr>
			      <tr>
			        <td>Exp Date:</td>
			        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="cc_info[exp_date]" value="<?php echo $cc_info->exp_date;?>" /></td>
			        <td>Address:</td>
			        <td align="right"><input type="text" name="cc_info[address]" id="address" value="<?php echo $cc_info->address;?>" /></td>
			      </tr>
			      <tr>
			        <td>CV2:</td>
			        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="cc_info[cv2]" value="<?php echo $cc_info->cv2;?>" /></td>
			        <td>Zip code:</td>
			        <td align="right"><input type="text" name="cc_info[zipcode]" id="zip_code" value="<?php echo $cc_info->zipcode;?>" /></td>
			      </tr>
			    </table></td>
			  </tr>
			  <tr><td>&nbsp;</td></tr>
			  <tr>
			    <td>
			    <table width="100%" border="0">
			    <?php 
			    foreach($payments as $key=>$payment){
			    	$payment_number = $key+1;
			    ?>
			      <tr>
			        <td width="10%">Payment #<?php echo $payment_number;?>:</td>
			        <td width="20%">on <input type="text" name="payment_date[]" class="date_picker" value="<?php echo $payment->date;?>" /></td>
			        <td>
			        	Method: 
			            <select name="payment_method[]">
			            <option value="cc">CC</option>
			            </select>
			        </td>
			        <td width="15%" align="right">Amount:          
			          <input type="text" name="payment_amount[]" size="10" value="<?php echo $payment->amount;?>" /></td>
			      </tr>
			      <tr>
			        <td colspan="4"><hr /></td>
			      </tr>
			    <?php 
			    }
			    ?>
			      <tr>
			        <td width="10%">Payment #<?php echo ++$payment_number;?>:</td>
			        <td width="20%">on <input type="text" name="payment_date[]" class="date_picker" /></td>
			        <td>
			        	Method: 
			            <select name="payment_method[]">
			            <option value="cc">CC</option>
			            </select>
			        </td>
			        <td width="15%" align="right">Amount:          
			          <input type="text" name="payment_amount[]" size="10" /></td>
			      </tr>
			      <tr>
			        <td colspan="4"><hr /></td>
			      </tr>
			      <tr>
			        <td>Total: </td>
			        <td>&nbsp;</td>
			        <td>&nbsp;</td>
			        <td align="right">Amount:
			          <input type="text" name="payment_total" size="10" value="<?php echo $bookings->total_paid;?>" readonly />
			        </td>
			      </tr>
			    </table>
			    </td>
			  </tr>
			</table>
		</div>
		<div id="guest_info">
			<table width="100%" border="0">
			  <tr>
			    <td colspan="2"><b>Reference: <?php echo $bookings->id;?></b></td>
			  </tr>
			  <tr>
			    <td colspan="2">Guest Information:<hr /></td>
			  </tr>
			  <tr>
			    <td width="20%">Name:</td>
			    <td>
			    <select name="customer[title]" id="title" size="1">
						<option value="Mr" <?php echo ($guest->title == "Mr" ? 'selected="selected"' : '' );?>>Mr</option>
						<option value="Mrs" <?php echo ($guest->title == "Mrs" ? 'selected="selected"' : '' );?>>Mrs</option>
						<option value="Miss" <?php echo ($guest->title == "Miss" ? 'selected="selected"' : '' );?>>Miss</option>
						<option value="Ms" <?php echo ($guest->title == "Ms" ? 'selected="selected"' : '' );?>>Ms</option>
				</select>
				<input type="text" name="customer[firstname]" size="20" value="<?php echo $guest->firstname; ?>" />
				<input type="text" name="customer[surname]" size="20" value="<?php echo $guest->surname; ?>" />
			    </td>
			  </tr>
			  <tr>
			    <td>Address:</td>
			    <td><input type="text" name="customer[address1]" size="40" value="<?php echo $guest->address1; ?>" /></td>
			  </tr>
			  <tr>
			    <td>Other Address:</td>
			    <td><input type="text" name="customer[address2]" size="40" value="<?php echo $guest->address2; ?>" /></td>
			  </tr>
			  <tr>
			    <td>City:</td>
			    <td><input type="text" name="customer[city]" size="40" value="<?php echo $guest->city; ?>" /></td>
			  </tr>
			  <tr>
			    <td>State/Country:</td>
			    <td><input type="text" name="customer[state]" size="40" value="<?php echo $guest->state; ?>" /></td>
			  </tr>
			  <tr>
			    <td>Country:</td>
			    <td><input type="text" name="customer[country]" size="40" value="<?php echo $guest->country; ?>" /></td>
			  </tr>
			  <tr>
			    <td>Postcode:</td>
			    <td><input type="text" name="customer[postcode]" size="40" value="<?php echo $guest->postcode; ?>" /></td>
			  </tr>
			  <tr>
			    <td>Email:</td>
			    <td><input type="text" name="customer[email]" size="40" value="<?php echo $guest->email; ?>" /></td>
			  </tr>
			  <tr>
			    <td>Telephone:</td>
			    <td><input type="text" name="customer[telephone]" size="40" value="<?php echo $guest->telephone; ?>" /></td>
			  </tr>
			  <tr>
			    <td colspan="2"><hr /></td>
			  </tr>
			  <tr>
			    <td><b>Adults: </b></td>
			    <td>&nbsp;</td>
			  </tr>
			  <tr>
			    <td colspan="2">
			    	<table width="100%" border="0" id="adults_table">
			  <?php foreach($adults as $adult){
			  $adult_info = explode(",", $adult);
			  ?>
			      		<tr>
			        		<td width="20%">Name: <input type="text" name="adults[name][]" value="<?php echo $adult_info[0];?>" /></td>
			        		<td>Age: <input type="text" name="adults[age][]" value="<?php echo $adult_info[1];?>" /></td>
			    		</tr>
			  <?php }?>
			    	</table>
			    </td>
			  </tr>
			  <tr>
			    <td colspan="2">
			    	<a href="#" onClick="addFormField('adults'); return false;">Add Another Row</a>
			    	<input type="hidden" id="adults_id" value="1">
			    </td>
			  </tr>
			  <tr>
			    <td colspan="2">&nbsp;</td>
			  </tr>
			  <tr>
			    <td><b>Children: </b></td>
			    <td>&nbsp;</td>
			  </tr>
			  <tr>
			    <td colspan="2">
			    	<table width="100%" border="0" id="children_table">
			  <?php foreach($children as $child){
			  $child_info = explode(",", $child);
			  ?>
			      		<tr>
			        		<td width="20%">Name: <input type="text" name="children[name][]" value="<?php echo $child_info[0];?>" /></td>
			        		<td>Age: <input type="text" name="children[age][]" value="<?php echo $child_info[1];?>" /></td>
			    		</tr>
			  <?php }?>
			    	</table>
			    </td>
			  </tr>
			  <tr>
			    <td colspan="2">
			    	<a href="#" onClick="addFormField('children'); return false;">Add Another Row</a>
			    	<input type="hidden" id="children_id" value="1">
			    </td>
			  </tr>
			  <tr>
			    <td colspan="2">&nbsp;</td>
			  </tr>
			  <tr>
			    <td colspan="2"><hr /></td>
			  </tr>
			</table>
		</div>
		<div id="set_letters">
			<table width="80%" border="0" class="Centered">
			  <tr>
			    <td colspan="5" class="LeftAlign"><b>Reference: <?php echo $bookings->id;?></b></td>
			  </tr>
			  <tr>
			    <th colspan="5"><span id="EmailMsg"></span></th>
			  </tr>
			  <tr>
			    <th colspan="5" class="LeftAlign"><h2>Set Letters</h2></th>
			  </tr>
			  <tr class="BottomBordered">
			    <th width="10%">Number</th>
			    <th width="30%">Identifier</th>
			    <th width="20%">To</th>
			    <th width="20%">Email</th>
			    <th width="20%">Print</th>
			  </tr>
			  <?php if(!empty($letters)){?>
			  	<?php foreach($letters as $key=>$letter){?>
			  <tr>
			    <td>Letter #<?php echo ($key+1);?></td>
			    <td><input class="Identifier" type="text" name="identifier" value="<?php echo $letter->identifier;?>" readonly /></td>
			    <td>
			    	<select id="send_to" name="send_to">
			        	<option value="GUEST">Guest</option>
			            <option value="OWNER">Home Owner</option>
			        </select>
			    </td>
			    <td><input class="Wide" type="button" value="Email" onclick="send_mail('<?php echo $letter->id;?>')" /></td>
			    <td><input class="Wide" type="button" name="" value="Print" onclick="show_print('<?php echo $letter->id;?>')" /></td>
			  </tr>
			  	<?php }?>
			  <?php }?>
			</table>
		</div>
	</div>
	<input type="hidden" name="form" value="edit_booking">
	</form>
	</td></tr>
</table>
<?php $KToolBar->addLink('<a href="#" onclick="document.edit_booking_form.submit()"><img border="0" alt="Save" src="images/save_f2.png"/><br />Save</a>');?>
<?php $KToolBar->addLink('<a href="?option='.COMPONENT_NAME.'&page=bookings"><img border="0" alt="Cancel" src="images/cancel_f2.png"/><br />Cancel</a>');?>

<script type="text/javascript" src="<?php echo COMPONENT_PATH."js/jquery.history_remote.js"; ?>" charset="ISO-8859-1"></script>
<script type="text/javascript" src="<?php echo COMPONENT_PATH."js/jquery.tabs.js"; ?>" charset="ISO-8859-1"></script>
<script type="text/javascript" src="<?php echo COMPONENT_PATH."js/date.js"; ?>" charset="ISO-8859-1"></script>
<script type="text/javascript" src="<?php echo COMPONENT_PATH."js/jquery.datePicker.js"; ?>" charset="ISO-8859-1"></script>
<script type="text/javascript">
Date.firstDayOfWeek = 0;
Date.format = 'mm/dd/yyyy';
$(function()
{
	$('.date_pick').datePicker().dpSetStartDate('01/01/1900');
	$('#arr_date').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#dep_date').dpSetStartDate(d.addDays(1).asString());
			}
		}
	);
	$('#dep_date').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#arr_date').dpSetEndDate(d.addDays(-1).asString());
			}
		}
	);
});
function addFormField(group) {
	var id = document.getElementById(group+"_id").value;
	$("#"+group+"_table").append("<tr id='"+group+"_row" + id + "'><td>Name:&nbsp;<input type='text' size='20' name='"+group+"[name][]' /></td><td>Age: <input type='text' size='20' name='"+group+"[age][]' />&nbsp;&nbsp<a href='#' onClick=\"removeFormField('"+group+"_row" + id + "'); return false;\">Remove</a></td></tr>");
	//$( group + "_row" + id).highlightFade({speed:1000});
	id = (id - 1) + 2;
	document.getElementById(group+"_id").value = id;
}

function removeFormField(id) {
	$('#'+id).remove();
}
function send_mail(lid){
	if(confirm("Are you sure to send the mail?")){
		$("#EmailMsg").text("Sending Mail...");
		var to = $('#send_to').val();
		$.post("<?php echo COMPONENT_PATH?>functions/send_email.php", {lid: lid, to: to, bid: <?php echo $bookings->id;?>, cid: <?php echo $bookings->cid;?>, uid: <?php echo $bookings->uid;?>}, function(data){
			if(data == "success") $("#EmailMsg").text("Email sent");
			else $("#EmailMsg").text("Email sending failed");
		});
	}
}
function show_print(id){
	var to = $('#send_to').val();
	window.open("<?php echo COMPONENT_PATH;?>functions/printer.php?lid="+ id + "&to="+ to + "&bid=<?php echo $bookings->id;?>&uid=<?php echo $bookings->uid;?>&cid=<?php echo $bookings->cid;?>", "_blank");
}
$(function() {
	$('.date_picker').datePicker();
    $('#bookings_tabs').tabs();
});
</script>
